package com.cdks.wuxiu.api.service;


import com.cdks.wuxiu.api.component.cdks.kmsg.SingleMessage;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.component.constant.ScoreEnum;
import com.cdks.wuxiu.api.model.dto.UserScoreDTO;
import com.cdks.wuxiu.api.service.mysql.ScoreAwardConfigService;
import com.cdks.wuxiu.api.service.mysql.UserBillInfoService;
import com.cdks.wuxiu.api.service.mysql.UserScoreInfoService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.entity.message.ChatPrivateData;
import com.cdks.wuxiu.entity.mysql.ScoreAwardConfig;
import com.cdks.wuxiu.entity.mysql.UserBillInfo;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;


/**
 * 积分服务实现类
 */
@Slf4j
@Service
public class ScoreServiceImpl implements ScoreService {
    @Resource
    private UserBillInfoService userBillInfoService;
    @Resource
    private UserScoreInfoService userScoreInfoService;
    @Resource
    private ScoreAwardConfigService scoreAwardConfigService;    


    private int saveScoreBill(long cost, long earnerId, String remark)
    {
        int ret  = userBillInfoService.saveBill(cost, cost, 0,  cost, cost, Constant.USERID_SYSTEM, earnerId, Constant.COST_TYPE_SCORE, Constant.COST_TYPE_SCORE, 0, remark);
        if (ret < 0)
        {
            log.error("score reward save bill failed" );

            return -1;
        }

        return 0;
    }

    @Override
    public int rewardScore(long userId, ScoreEnum type)
    {
        ScoreAwardConfig config  = scoreAwardConfigService.getByType(type.getCode());
        if (config == null)
        {
            return -1;
        }

        //给用户发放奖励
        long expire  = TimeUtils.getTimeSecond() + config.getExpire();
        userScoreInfoService.addScore(userId, config.getScore(), type.getCode(), expire, type.getDesc());
        //保存用户积分账单
        saveScoreBill(config.getScore(), userId, type.getDesc());

        //给用户发送通知
        ChatPrivateData data  = new ChatPrivateData();
        data.setContent("您有" + config.getScore() + "积分到账，可提现也可消费（语音通话第一分钟不能使用），有效期至" + TimeUtils.getLocalDateTime("yyyy-MM-dd HH:mm:ss", expire));
        SingleMessage.sendChatMessage(Constant.USERID_SYSTEM, userId, Constant.MSG_TYPE_TEXT, Constant.ENABLE_NO, data);

        return 0;
    }

    @Override
    public List<UserBillInfo> getRecord(long userId) {
        long now  = TimeUtils.getTimeSecond();
        long start  = (now / 86400) * 86400 - 28800;

        return getRecord(userId, 0, start, now);
    }

    @Override
    public List<UserBillInfo> getRecord(long userId, int page, long start, long stop) {
        return userBillInfoService.getByUserIdOnlyScore(userId, page, start, stop);
    }

    @Override
    public long getBalance(long userId) {
        UserScoreDTO score  = userScoreInfoService.getBalanceByUser(userId, TimeUtils.getTimeSecond());
        if (score == null)
        {
            return 0;
        }

        return score.getScore();
    }
}
